/*
 * Author: xiguan
 * Mail: xiguan.teng@qq.com
 * -----
 * Last Modified: 2022-06-10 14:31:21
 * Modified By: xiguan
 * -----
 * Copyright (c) 2022 XiGuan Inc.
 * 
 * No Bug No error No warning.
 */



#include <iostream>
#include <sys/stat.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[]){

    if(argc < 3){
        printf("Usage: %s <mode num> <file>\n", argv[0]);
        exit(0);
    }

    int mode_in = atoi(argv[1]);
    if(mode_in > 777 || mode_in < 0){
        cout << "mode num error" << endl;
        exit(0);
    }
    int mode_u = mode_in/100, mode_g = mode_in%100/10, mode_o = mode_in%10;

    
    mode_in = (mode_u*8*8)+(mode_g*8)+mode_o;

    char *file_path = argv[2];
    if(chmod(file_path, mode_in) == -1){
        fputs("chmod error", stderr);
        fputc('\n', stderr);
        exit(1);
        
    }

    return 0;
}